package st.strategy.vo;

import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.stat.descriptive.SynchronizedDescriptiveStatistics;

import st.BasicStockData;
import st.base.Sysdate;

/** 表一檔股檔的MV */
public class Mv implements TechLine {
	private int n = 5;
	private Collection<BasicStockData> stockDatas;
	SynchronizedDescriptiveStatistics ds;
	Map<Sysdate, SimpleTechValue> data;

	public Mv(Collection<BasicStockData> stockDatas) {
		this.stockDatas = stockDatas;
		ds = new SynchronizedDescriptiveStatistics(n);
		data = new HashMap<Sysdate, SimpleTechValue>();
		init();
	}

	private void init() {
		for (BasicStockData stockData : stockDatas) {
			ds.addValue(stockData.getClose());
			double[] values = ds.getValues();
			double mean = StatUtils.mean(values);
			SimpleTechValue tv = new SimpleTechValue();
			tv.setValue(mean);
			data.put(stockData.getTradeDate(), tv);
		}
	}

	public TechValue getValue(Sysdate date) {
		return data.get(date);
	}

}
